﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;

/// <summary>
/// Summary description for HinhAnhDA
/// </summary>
public class HinhAnhDA
{
    Connection con = new Connection();
    public HinhAnh getTop1bySPID(String id)
    {
        try
        {
            SqlCommand com = new SqlCommand("HinhAnh_getbySanPhamID", con.getCon());
            com.CommandType = CommandType.StoredProcedure;
            com.Parameters.Add("@ID", SqlDbType.VarChar, 20).Value = id;
            con.openCon();
            SqlDataReader dr = com.ExecuteReader();
            HinhAnh ha = new HinhAnh();
            if (dr.Read())
            {
                ha.Id = Convert.ToInt32(dr["ID"].ToString());
                ha.Masanpham = id;
                ha.Url = dr["URL"].ToString();
            }
            con.closeCon();
            return ha;
        }
        catch (Exception)
        {

            throw;
        }
        finally {
            con.closeCon();
        }
    }
    public void insert(string masanpham, string filename)
    {
        try
        {
            SqlCommand com = new SqlCommand("insert into HinhAnh(URL,MaSanPham) values(@filename,@masanpham)", con.getCon());
            com.Parameters.Add("@masanpham", SqlDbType.NVarChar, 20).Value = masanpham;
            com.Parameters.Add("@filename", SqlDbType.VarChar, 50).Value = filename;
            con.openCon();
            com.ExecuteNonQuery();
            con.closeCon();
        }
        catch (Exception)
        {

            throw;
        }
        finally {
            con.closeCon();
        }

    }
}